Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

32-H0ngJu #252

Merged
merged 1 commit into from
Nov 19, 2024
Merged

32-H0ngJu #252

merged 1 commit into from
Nov 19, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Oct 13, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๊ธฐํƒ€ ๋ ˆ์Šจ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„?

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

image

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์‰ฝ์Šต๋‹ˆ๋‹ค




๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด๋ฉด, '๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ์„œ ์ˆ˜๊ฐ€ ์ •ํ•ด์ง€๊ฒ ๊ตฌ๋‚˜'๋ฅผ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.



๋ธ”๋ฃจ๋ ˆ์ด์˜ ์ˆ˜๋Š” 1๋ถ€ํ„ฐ 100000๊นŒ์ง€ ๊ฐ€๋Šฅํ•˜๊ณ 
1๋ถ€ํ„ฐ 100000๊นŒ์ง€ ๋‹ค ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์€ ๋ฌด๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ธ”๋ฃจ๋ ˆ์ด๊ฐ€ 1๊ฐœ๋ผ๋ฉด ํฌ๊ธฐ๋Š” sum(๊ฐ ๊ฐ•์˜์˜ ๊ธธ์ด)์ผ๊ฑฐ๊ณ ,
๋ธ”๋ฃจ๋ ˆ์ด๊ฐ€ ๊ฐ•์˜์˜ ์ˆ˜๋งŒํผ ์žˆ๋‹ค๋ฉด, ํฌ๊ธฐ๋Š” max(๊ฐ•์˜ ๊ธธ์ด) ์ผ ๊ฒƒ์œผ๋กœ ๋ฒ”์œ„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ํƒ์ƒ‰ ์‹œ๊ฐ„์„ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ
์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ์ ‘๊ทผํ•˜์˜€์Šต๋‹ˆ๋‹ค.




๋ธ”๋ฃจ๋ ˆ์ด ํฌ๊ธฐ ์ตœ์†Œ = left = max(lecture)
๋ธ”๋ฃจ๋ ˆ์ด ํฌ๊ธฐ ์ตœ๋Œ€ = right = sum(lecture)
๋ธŒ๋ฃจ๋ ˆ์ด ํฌ๊ธฐ = mid = (left+right) // 2
๋กœ ์žก์•˜์Šต๋‹ˆ๋‹ค.

์ด์ œ ๊ฐ•์˜๋“ค์„ ํ•˜๋‚˜์”ฉ ๋„ฃ๊ณ , mid(๋ธ”๋ฃจ๋ ˆ์ด ํฌ๊ธฐ)๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด ๋ ˆ์ฝ”๋“œ ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋Š˜๋ ค๊ฐ‘๋‹ˆ๋‹ค.

์ตœ์ข… ๋ ˆ์ฝ”๋“œ ์ˆ˜๊ฐ€ M๋ณด๋‹ค ํฌ๋ฉด left๋ฅผ ๋Š˜๋ ค์„œ ๋” ๋งŽ์€ ๊ฐ•์˜๋ฅผ ๋‹ด๋„๋ก ํ•˜๊ณ ,

๋ฐ˜๋Œ€์˜ ์ƒํ™ฉ์ด๋ฉด rigth๋ฅผ ์ค„์—ฌ์„œ ๋ธ”๋ฃจ๋ ˆ์ด ๋ฒ”์œ„๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค!

์ตœ์ข…์ ์œผ๋กœ left๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋ธ”๋ฃจ๋ ˆ์ด ํฌ๊ธฐ์˜ ์ตœ์†Ÿ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค



์ž๋ฃŒ๊ตฌ์กฐ์˜€๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋˜ ๊ธฐ์–ต์ด ์žˆ์–ด์„œ
์‰ฝ๊ฒŒ ์ ‘๊ทผํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”


import sys

def input() : return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
lecture = list(map(int, input().split()))

left = max(lecture)
right = sum(lecture)

while left <= right:
    mid = (left+right)//2
    record_cnt = 1
    time = 0 # ๋ ˆ์ฝ”๋“œ ํ•˜๋‚˜์— ๋“ค์–ด๊ฐ€๋Š” ์‹œ๊ฐ„

    for i in range(N):
        if time + lecture[i] > mid: # ์‹œ๊ฐ„ ์ดˆ๊ณผ๋˜๋ฉด ๋‹ค์Œ ๋ ˆ์ฝ”๋“œ๋กœ
            record_cnt += 1
            time = lecture[i]
        else:
            time += lecture[i]

    if record_cnt <= M: # ๊น…์˜๊ฐ€ ๋‚จ์•„์žˆ์œผ๋ฉด ํฌ๊ธฐ ์ค„์ด๊ธฐ
        right = mid - 1
    else:
        left = mid + 1

print(left)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import sys

def input(): return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
lessons = list(map(int, input().split()))

def check(lessons, mid, M):
    now = 0

    for lesson in lessons:
        if lesson > mid: return False ## <--- ์ด ๋ถ€๋ถ„
        
        if mid >= lesson + now:
            now += lesson
        else:
            now = lesson
            M -= 1
            if M < 0: return False
    return True

left = 0
right = 1_000_000_001
answer = 0
while left+1 < right:
    mid = (left + right) // 2
    if (check(lessons, mid, M-1)):
        answer = mid
        right = mid
    else: 
        left = mid

print(answer)

์ €๋„ ํ™์ฅฌ๋ฅด๋ž‘ ์ ‘๊ทผ์€ ๋ฐ”๋กœ ๋˜ญ ๋– ์˜ฌ๋ž๋Š”๋ฐ,
๋ ˆ์Šจ์˜ ํ•˜๋‚˜์˜ ๊ธธ์ด๊ฐ€ ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ๋ณด๋‹ค ์ปธ์„ ๋•Œ์˜ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ์ง€ ๋ชปํ•ด์„œ ์กฐ๊ธˆ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.
ํ˜น์‹œ ํ™์ฅฌ๋ฅด์˜ ์ฝ”๋“œ์—์„œ๋Š” ๊ทธ๊ฒŒ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋˜๊ณ  ์žˆ๋‚˜์š” ?

์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋ถ„ํƒ์ƒ‰์—์„œ ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ๋ฅผ 5๋กœ mid๊ฐ’์ด ๋‚˜์™”์„ ๋•Œ
ํƒ์ƒ‰ํ•˜๋Š” ๊ฐ•์˜์˜ ๋‹จ์ผ ๊ธธ์ด๊ฐ€ 10์ผ ๊ฒฝ์šฐ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋˜๋Š” ์ง€ ๊ถ๊ธˆํ•ด์š”.

@H0ngJu
Copy link
Collaborator Author

H0ngJu commented Nov 19, 2024

import sys

def input(): return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
lessons = list(map(int, input().split()))

def check(lessons, mid, M):
    now = 0

    for lesson in lessons:
        if lesson > mid: return False ## <--- ์ด ๋ถ€๋ถ„
        
        if mid >= lesson + now:
            now += lesson
        else:
            now = lesson
            M -= 1
            if M < 0: return False
    return True

left = 0
right = 1_000_000_001
answer = 0
while left+1 < right:
    mid = (left + right) // 2
    if (check(lessons, mid, M-1)):
        answer = mid
        right = mid
    else: 
        left = mid

print(answer)

์ €๋„ ํ™์ฅฌ๋ฅด๋ž‘ ์ ‘๊ทผ์€ ๋ฐ”๋กœ ๋˜ญ ๋– ์˜ฌ๋ž๋Š”๋ฐ, ๋ ˆ์Šจ์˜ ํ•˜๋‚˜์˜ ๊ธธ์ด๊ฐ€ ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ๋ณด๋‹ค ์ปธ์„ ๋•Œ์˜ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ์ง€ ๋ชปํ•ด์„œ ์กฐ๊ธˆ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ํ™์ฅฌ๋ฅด์˜ ์ฝ”๋“œ์—์„œ๋Š” ๊ทธ๊ฒŒ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋˜๊ณ  ์žˆ๋‚˜์š” ?

์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋ถ„ํƒ์ƒ‰์—์„œ ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ๋ฅผ 5๋กœ mid๊ฐ’์ด ๋‚˜์™”์„ ๋•Œ ํƒ์ƒ‰ํ•˜๋Š” ๊ฐ•์˜์˜ ๋‹จ์ผ ๊ธธ์ด๊ฐ€ 10์ผ ๊ฒฝ์šฐ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋˜๋Š” ์ง€ ๊ถ๊ธˆํ•ด์š”.

๋‹ค์‹œ ๋ฌธ์ œ+์ฝ”๋“œ๋ฅผ ์ •๋…ํ•˜๊ณ  ์™”์Šต๋‹ˆ๋‹ค

ํƒœ๊ทœ๋‹˜ ์ฝ”๋“œ๋Š” ๋‹จ์ผ ๋ ˆ์ฝ”๋“œ๊ฐ€ mid๋ณด๋‹ค ํฌ๋ฉด check๊ฐ€ False๋กœ ๋ฐ˜ํ™˜๋˜์–ด ์™ผ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์ฆ๊ฐ€ ์‹œํ‚ค๋Š” ๊ฒƒ ๊ฐ™์•„์š”.

์ œ ์ฝ”๋“œ์—์„œ๋Š” ํ˜„์žฌ ๋ธ”๋ฃจ๋ ˆ์ดํฌ๊ธฐ(mid)๋ณด๋‹ค ๋‹จ์ผ ๊ฐ•์˜๊ฐ€ ํฌ๋ฉด,
record_cnt๋ฅผ ๋Š˜๋ ค์คฌ๊ณ , ์ด record_cnt์™€ M์„ ๋‹ค์‹œ ๋น„๊ตํ•˜์—ฌ mid(ํ˜„์žฌ ๋ธ”๋ฃจ๋ ˆ์ด ํฌ๊ธฐ)๋ฅผ ์กฐ์ ˆํ•˜๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

for i in range(N):
        if time + lecture[i] > mid: # ์‹œ๊ฐ„ ์ดˆ๊ณผ๋˜๋ฉด ๋‹ค์Œ ๋ ˆ์ฝ”๋“œ๋กœ
            record_cnt += 1
            time = lecture[i]

์š” ๋ถ€๋ถ„์ด ๋˜๊ฒ ๋„ค์šฉ

@H0ngJu H0ngJu merged commit 455c6da into main Nov 19, 2024
9 checks passed
@H0ngJu H0ngJu deleted the 32-H0ngJu branch November 19, 2024 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants